package src.com.atguigu02.iterator;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * ClassName: PersonTest
 * Description:
 *
 * @Author liu
 * @Create 2024/11/24 15:45
 * @Version 1.0
 */
public class IteratorTest {
    @Test
    public void test(){
        Collection coll = new ArrayList();

        coll.add("AA");
        coll.add("AA");
        Person p1 = new Person("Tom",12);
        coll.add(p1);
        coll.add(128);//自动装箱
        coll.add(new String("谷"));


        //获取Iterator的的对象
        Iterator iterator = coll.iterator();
    //    System.out.println(iterator.getClass());

        //方式一；有几个add就有几个next()
        /*System.out.println(iterator.next());
        System.out.println(iterator.next());
        System.out.println(iterator.next());
        System.out.println(iterator.next());
        System.out.println(iterator.next());
*/

        //方法二：for循环
        for (int i = 0; i < coll.size(); i++) {
            System.out.println(iterator.next());
        }

        //方式三：推荐
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }


    }
}
